package com.iqiju.guagua.photo.wx.mini.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicResponseParameters;
import com.iqiju.guagua.photo.wx.mini.common.Results;
import com.iqiju.guagua.photo.wx.mini.service.WxMiniUserService;
import com.iqiju.guagua.photo.wx.mini.vo.LoginVO;
import com.iqiju.guagua.photo.wx.mini.vo.UserAuthVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@Api(tags = "权限接口")
public class LoginController {

    @Autowired
    private WxMiniUserService wxMiniUserService;





    @ApiImplicitParams({
            @ApiImplicitParam(name = "wxMiniAppId",value = "小程序自定义ID",required = true,dataType = "String",paramType = "path",example = "1192388834980298754"),
            @ApiImplicitParam(name = "code",value = "登录时获取的 code",required = true,dataType = "String",paramType = "path",example = "a1s2d3"),
            @ApiImplicitParam(name = "channel",value = "分享渠道id（没有默认0）",required = true,dataType = "String",paramType = "path",example = "0"),
            @ApiImplicitParam(name = "shareUserId",value = "分享用户id（没有默认0）",required = true,dataType = "String",paramType = "path",example = "0")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "接口返回成功"),
            @ApiResponse(code = 100, message = "参数传递错误，检查参数是否合法"),
            @ApiResponse(code = 200, message = "接口返回未知错误，请联系开发人员调试"),
            @ApiResponse(code = 500, message = "接口返回未知错误，请联系开发人员调试")
    })
    @ApiOperation(value = "登录接口", notes = "访问此接口，进行微信小程序简单登录")
    @PostMapping("/wx/mini/login/{wxMiniAppId}/{code}/{channel}/{shareUserId}")
    public Results<LoginVO> codeLogin(@PathVariable String wxMiniAppId, @PathVariable String code,
                                      @PathVariable String channel, @PathVariable String shareUserId){
        log.info("访问登录接口--------------------------------------------------");
        return wxMiniUserService.login(wxMiniAppId, code, channel, shareUserId);
    }


//    @PostMapping("/wx/mini/login2/{wxMiniAppId}")
//    public Results<LoginVO> codeLogin2(@PathVariable String wxMiniAppId){
//        log.info("访问登录接口--------------------------------------------------");
//        return wxMiniUserService.login2(wxMiniAppId);
//    }
















    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "String",paramType = "path",example = "123"),
    })
    @ApiOperationSupport(
            responses = @DynamicResponseParameters(properties = {
                    @DynamicParameter(value = "返回结果码",name = "state",dataTypeClass = Integer.class,example = "0",required = true),
                    @DynamicParameter(value = "返回提示信息",name = "message",dataTypeClass = String.class,example = "SUCCESS",required = true),
                    @DynamicParameter(value = "访问接口次数（0或1）",name = "content",dataTypeClass = Long.class),
                    @DynamicParameter(value = "时间戳",name = "time",dataTypeClass = Long.class,example = "1567425139000",required = true)
            })
    )
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "接口返回成功"),
            @ApiResponse(code = 100, message = "参数传递错误，检查参数是否合法"),
            @ApiResponse(code = 405, message = "token错误"),
            @ApiResponse(code = 401, message = "登录过期或者未登录"),
            @ApiResponse(code = 200, message = "接口返回未知错误，请联系开发人员调试"),
            @ApiResponse(code = 500, message = "接口返回未知错误，请联系开发人员调试")
    })
    @ApiOperation(value = "当天是否已经访问过授权接口", notes = "访问此接口，返回今日是否已经访问过授权标识,content 返回0表示今天访问授权接口次数为0，可以访问，返回1表示今日已经访问过，不用再次访问")
    @GetMapping("/wx/mini/have/auth/{userId}")
    public Results haveAuthToday(@PathVariable String userId){
        log.info("访问当天是否已经访问过授权接口--------------------------------------------------");
        return wxMiniUserService.haveAuthToday(userId);

    }




















    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "接口返回成功"),
            @ApiResponse(code = 100, message = "参数传递错误，检查参数是否合法"),
            @ApiResponse(code = 405, message = "token错误"),
            @ApiResponse(code = 401, message = "登录过期或者未登录"),
            @ApiResponse(code = 200, message = "接口返回未知错误，请联系开发人员调试"),
            @ApiResponse(code = 500, message = "接口返回未知错误，请联系开发人员调试")
    })
    @ApiOperation(value = "授权接口", notes = "访问此接口，进行微信小程序授权")
    @PostMapping("/wx/mini/auth")
    public Results auth(@RequestBody UserAuthVO userAuthVO){
        log.info("访问授权接口--------------------------------------------------");
        return wxMiniUserService.auth(userAuthVO);
    }
}
